package com.sicnu.cheer.profileset.activity;

import android.app.ProgressDialog;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.sicnu.cheer.generalmodule.activity.BaseActivity;
import com.sicnu.cheer.generalmodule.util.StringUtils;
import com.sicnu.cheer.generalmodule.util.UIHelper;
import com.sicnu.cheer.generalmodule.util.ValidateUtil;
import com.sicnu.cheer.profileset.R;
import com.sicnu.cheer.profileset.bean.FeedbackBean;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.SaveListener;

public class FeedbackActivity extends BaseActivity {

    private EditText suggestionET;
    private TextView charCountTV;
    private EditText contactNumberET;

    @Override
    public int getContentViewId() {
        return R.layout.activity_feedback;
    }

    @Override
    protected void initView() {
        super.initView();
        ((TextView) findViewById(R.id.title)).setText("意见反馈");
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        suggestionET = ((EditText) findViewById(R.id.suggestion_et));
        charCountTV = ((TextView) findViewById(R.id.character_count_tv));
        contactNumberET = ((EditText) findViewById(R.id.contact_number_et));
        suggestionET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                charCountTV.setText(suggestionET.getText().toString().length()+"/300");
            }
        });
        findViewById(R.id.suggestion_submit_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckAndSubmit();
            }
        });
    }

    private void CheckAndSubmit() {
        String content=suggestionET.getText().toString().trim();
        String phone=contactNumberET.getText().toString().trim();
        if (StringUtils.isEmpty(content)){
            UIHelper.ToastMessage(mThis,"请输入意见建议！");
            return;
        }
        if (!StringUtils.isEmpty(phone)&& !ValidateUtil.isMobile(phone)){
            UIHelper.ToastMessage(mThis,"手机号码格式有误！");
            return;
        }
        submit(content,phone);

    }

    private void submit(String content, String phone) {
        ProgressDialog progressDialog=new ProgressDialog(mThis);
        progressDialog.setMessage("加载中...");
        progressDialog.show();

        FeedbackBean bean = new FeedbackBean();
        bean.setUserID(BmobUser.getCurrentUser(mThis).getObjectId());
        bean.setContent(content);
        bean.setUserPhone(phone);
        bean.save(mThis, new SaveListener() {
            @Override
            public void onSuccess() {
                UIHelper.ToastMessage(mThis,"意见建议提交成功");
                finish();
            }

            @Override
            public void onFailure(int code, String arg0) {
                UIHelper.ToastMessage(mThis,"意见建议提交失败:"+arg0);
            }
        });
    }
}
